package com.example.healkep;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.Objects;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class IndexchangeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_indexchange);
        EditText icf = findViewById(R.id.cicf);
        EditText ecf = findViewById(R.id.cecf);
        EditText muscle = findViewById(R.id.cmuscle);
        EditText protein = findViewById(R.id.cprotein);
        EditText ffweight = findViewById(R.id.cffweight);
        EditText perfat = findViewById(R.id.cperfat);
        EditText brate = findViewById(R.id.cbrate);
        Button change = findViewById(R.id.changeindex);

        change.setOnClickListener(v->{
            String json = "{\n" +
                    "    \"id\":\"" + 123456 + "\",\n" +
                    "    \"icf\":\"" + icf.getText() + "\",\n" +
                    "    \"ecf\":\"" + ecf.getText() + "\",\n" +
                    "    \"muscle\":\"" + muscle.getText() + "\",\n" +
                    "    \"protein\":\"" + protein.getText() + "\",\n" +
                    "    \"ffweight\":\"" + ffweight.getText() + "\",\n" +
                    "    \"perfat\":\"" + perfat.getText() + "\",\n" +
                    "    \"brate\":\"" + brate.getText() + "\"\n" +
                    "}";
            new Thread(()->{
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(LoginActivity.url + "/index/update")
                            .addHeader("Cookie", LoginActivity.getSessionID())
                            .post(RequestBody.create(MediaType.parse("application/json"), json))
                            .build();
                    Response response = client.newCall(request).execute();
                }catch (Exception e){
                    runOnUiThread(() -> Toast.makeText(IndexchangeActivity.this, "连接异常", Toast.LENGTH_SHORT).show());
                }
            }).start();
        });
    }
}